/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sunsite.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sunsite.po.Resource;

/**
 *
 * @author mg
 */
public class ResourceDaoImpl extends HibernateDaoSupport implements ResourceDao{

    private final int SOFTWARE = 1;
    private final int OPENED_RESOURCE = 1;

    public List<Resource> getTop10Software() throws DataAccessException {
        List<Resource> list = getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from Resource as r where r.resourceType.resourceCatelog.catId=? and r.status=1 order by r.count desc").setParameter(0,SOFTWARE).setMaxResults(10).list();
            }
        });
        return list;
    }

    public List<Resource> getLatestResource() throws DataAccessException {
        List<Resource> list = getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from Resource as r where r.resourceType.resourceCatelog.resourceTp.tpId=? and r.status=1 order by r.uploadTime desc").setParameter(0,OPENED_RESOURCE).setMaxResults(10).list();
            }
        });
        System.out.println("list size:" + list.size());
        return list;
    }

}
